package com.yu.waterstation.system.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.waterstation.common.common.constant.Const;
import com.yu.waterstation.common.common.exception.ServiceException;

import com.yu.waterstation.common.common.util.GeneratorUtil;
import com.yu.waterstation.common.service.RedisService;
import com.yu.waterstation.system.modules.app.mapper.StudentMapper;
import com.yu.waterstation.common.entity.app.Student;
import com.yu.waterstation.common.entity.system.vo.StudentVO;
import com.yu.waterstation.system.modules.app.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentMapper studentMapper;
    @Resource
    private RedisService redisService;

    // 模糊查询
    public Page<StudentVO> searchStudents(String studentNo, String name, int pageNo, int pageSize) {
        Page<StudentVO> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Student> wrapper = new QueryWrapper<Student>();
        if (!StringUtils.isEmpty(studentNo))
            wrapper.like("username", studentNo);
        if (!StringUtils.isEmpty(name))
            wrapper.like("name", name);
        page.setTotal(studentMapper.selectCount(wrapper));
        page.setRecords(studentMapper.searchStudents( studentNo, name, (pageNo - 1) * pageSize, pageSize));
        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addStudent(Student student) throws Exception {
        Student studentInDB = studentMapper.selectOne(new QueryWrapper<Student>()
                .eq("student_no", student.getUsername()));
        if (studentInDB != null)
            throw ServiceException.CONST_studentNo_has_exists;

        student.setPassword(GeneratorUtil.md5Base64(Const.CONST_origin_password)); // 初始密码为123456
        student.setStatus(true);
        return studentMapper.insert(student);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteStudentBatchIds(List<Integer> studentIdList)   {
        return studentMapper.deleteBatchIds(studentIdList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateStudent(Student student) throws ServiceException {
        if (StringUtils.isEmpty(student.getUsername()) || student.getUsername().length() > 11) // 当要更新用户名时
                throw new ServiceException("用户名格式不正确");

        return studentMapper.updateById(student);
    }


    @Transactional(rollbackFor = Exception.class)
    public List<Map<String, Object>> getMembersOfAddress(Integer addressId) {
        Wrapper<Student> wrapper = new QueryWrapper<Student>()
                .select("id", "username", "name").eq("address_id", addressId); // student_no 没有映射
        return studentMapper.selectMaps(wrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateStudentStatus(Integer studentId) {
        Student student = studentMapper.selectById(studentId);
        student.setStatus(!student.getStatus());
        if(student.getStatus() != null && !student.getStatus()) // 冻结账号
            redisService.hdel(Const.CONST_student_session_map, studentId.toString()); // 删除学生会话信息
        return studentMapper.updateById(student);
    }

    @Transactional(rollbackFor = Exception.class)
    public int resetStudentPassword(Integer studentId) {
        Student student = studentMapper.selectById(studentId);
        student.setPassword(GeneratorUtil.md5Base64(Const.CONST_origin_password));
        return studentMapper.updateById(student);
    }
}
